﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace MvcControlsToolkit
{
    public class Slider: Control
    {
        public bool IsDisabled
        {
            get
            {
                if (Attributes.ContainsKey("disabled"))
                    return bool.Parse(Attributes["disabled"]);
                else return false;
            }
            set { Attributes["disabled"] = value.ToString().ToLower(); }
        }

        public bool IsAnimate 
        {
            get
            {
                if (Attributes.ContainsKey("animate"))
                    return bool.Parse(Attributes["animate"]);
                else return true;
            }
            set { Attributes["animate"] = value.ToString().ToLower(); }
        }

        public int MaxValue
        {
            get
            {
                if (Attributes.ContainsKey("max"))
                    return int.Parse(Attributes["max"]);
                else return 100;
            }
            set { Attributes["max"] = value.ToString().ToLower(); }
        }

        public int MinValue
        {
            get
            {
                if (Attributes.ContainsKey("min"))
                    return int.Parse(Attributes["min"]);
                else return 1;
            }
            set { Attributes["min"] = value.ToString().ToLower(); }
        }

        public string Orientation
        {
            get { return GetString("orientation"); }
            set { SetString("orientation", value); }
        }

        public string Range
        {
            get { return GetString("range"); }
            set { SetString("range", value); }
        }

        public int Step
        {
            get
            {
                if (Attributes.ContainsKey("step"))
                    return int.Parse(Attributes["step"]);
                else return 1;
            }
            set { Attributes["step"] = value.ToString().ToLower(); }
        }

        public int Value
        {
            get
            {
                if (Attributes.ContainsKey("value"))
                    return int.Parse(Attributes["value"]);
                else return 0;
            }
            set { Attributes["value"] = value.ToString().ToLower(); }
        }

        public int[] Values
        {
            get
            {
                if (Attributes.ContainsKey("values"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<int[]>(Attributes["values"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["values"] = result.ToLower();
            }
        }

        public string OnCreate
        {
            get { return Attributes["create"]; }
            set { Attributes["create"] = value; }
        }

        public string OnStart
        {
            get { return Attributes["start"]; }
            set { Attributes["start"] = value; }
        }

        public string OnSlide
        {
            get { return Attributes["slide"]; }
            set { Attributes["slide"] = value; }
        }

        public string OnChange
        {
            get { return Attributes["change"]; }
            set { Attributes["change"] = value; }
        }

        public string OnStop
        {
            get { return Attributes["stop"]; }
            set { Attributes["stop"] = value; }
        }

        /// <summary>
        /// Create slider control behavior
        /// </summary>
        /// <param name="name">Control name</param>
        public Slider(string name) : base(name)
        {
        }

        public override string GetJScript()
        {
            var result = new StringBuilder();

            foreach (string key in Attributes.Keys)
            {
                result.AppendFormat(", {0}: {1}", key, Attributes[key]);
            }

            if (result.Length > 0)
                result.Remove(0, 1);

            result.Append("});");

            result.Insert(0, "var " + Name + " = $(\"#" + Name + "\").slider({");

            return result.ToString();
        }

        public override string GetHtml()
        {
            var result = new StringBuilder();

            result.AppendFormat("<div id='{0}' name='{0}'></div>", Name);

            return result.ToString();
        }
    }
}
